//
//  MDHTableView.swift
//  MDHPT
//
//  Created by 高得华 on 2019/5/18.
//  Copyright © 2019年 高得华. All rights reserved.
//

import UIKit

//MARK： - ==== 基类 TableView
open class MDHTableView: UITableView, UIGestureRecognizerDelegate {

    ///顶部视图 是否可以点击
    @objc public var isThEnable = true
    ///多手势触发
    @objc public var isMulGestureEnable = true

    ///当前的点击事件 在TableHeader上是否需要响应
    override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if self.tableHeaderView != nil {
            let isPoint = self.tableHeaderView!.frame.contains(point)
            if isPoint {
                return self.isThEnable
            }
        }
        return super.point(inside: point, with: event)
    }
    
    //MARK: - ====  UIGestureRecognizerDelegate
    ///是否支持多手势触发，返回YES，则可以多个手势一起触发方法，返回NO则为互斥
    open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.isMulGestureEnable
    }
}


//MARK: - ==== 基类 TableViewCell
open class MDHTableViewCell: UITableViewCell, MDHReactiveView {
    
    ///分界线
    @objc public lazy var imvBottomLine: UIImageView = {
        let imv = MDHSwiftHeader.createUnifiedViewLine()
        self.addSubview(imv)
        imv.isHidden = true
        return imv
    }()
    
    ///类初始化
    @objc public class func initCell(tableView: UITableView, style: UITableViewCell.CellStyle = .subtitle) -> MDHTableViewCell {
        let id = NSStringFromClass(self.classForCoder())
        var cell = tableView.dequeueReusableCell(withIdentifier: id)
        if cell == nil {
            var iden = id
            let cbn = (Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "")+"."
            if iden.hasPrefix(cbn) {
                iden = (iden as NSString).substring(from: cbn.count)
            }
            let nibFilePath = Bundle.main.path(forResource: iden, ofType: "nib") ?? ""
            if nibFilePath.count > 0 {
                let nibs = Bundle.main.loadNibNamed(iden, owner: self, options: nil) ?? [Any]()
                cell = nibs.first as? UITableViewCell
            }
            if cell == nil {
                cell = self.init(style: style, reuseIdentifier: id)
            }
        }
        return cell as? MDHTableViewCell ?? MDHTableViewCell()
    }
    override required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none

        ///分界线
        self.imvBottomLine.mas_makeConstraints { (make) in
            make?.left.equalTo()(10)
            make?.centerX.bottom()?.equalTo()(0)
            make?.height.equalTo()(self.imvBottomLine.mh_height)
        }
    }
    public required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    ///绑定数据
    @objc open func bindViewModel(_ viewModel: Any!) { }
    
    /// 空操作
    @objc open func setIndexPath(indexPath: IndexPath, rowsInSection rows: Int) -> () { }
}


////MARK: - ===== 基类 MDHTableViewHeaderFooterView
open class MDHTableViewHeaderFooterView: UITableViewHeaderFooterView, MDHReactiveView {
    ///内容
    @objc public lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.color(hex: "#888888")
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.textAlignment = .left
        self.contentView.addSubview(label)
        return label
    }()
    
    ///类初始化
    @objc public class func initHFView(tableView: UITableView) -> MDHTableViewHeaderFooterView {
        let id = NSStringFromClass(self.classForCoder())
        var hf = tableView.dequeueReusableHeaderFooterView(withIdentifier: id)
        if hf == nil {
            hf = self.init(reuseIdentifier: id)
        }
        return hf as? MDHTableViewHeaderFooterView ?? MDHTableViewHeaderFooterView()
    }
    override required public init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
    }
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///绑定数据
    @objc open func bindViewModel(_ viewModel: Any!) { }
}


///顶部的TableHeaderView
open class MDHTableHeaderView: UIView, MDHReactiveView {
    @objc open func bindViewModel(_ viewModel: Any!) { }
}
/// 头部视图的ViewModel
open class MDHTableHeaderViewModel: NSObject {
    override public init() {
        super.init()
    }
    
    ///高度
    fileprivate var _rowHeight: CGFloat = 0.01
    /// rowHeight , default is 44.0f
    @objc public dynamic var height: CGFloat {
        set {
            _rowHeight = newValue
        }
        get {
            return ceil(_rowHeight*kMDHPTAutoScale())//+kMMWPTFC().kSafeAreaTop
        }
    }
}
